<template>
  <div class="login-container">
    <!-- navbar导航 -->
    <van-nav-bar class="page-nav-bar" title="登录">
      <!-- 左侧添加图标插槽，绑定路由回退方法 -->
      <van-icon slot="left" name="cross" @click="$router.back()" />
    </van-nav-bar>
    <!-- form表单区域 -->
    <!-- 表单验证：
    1.给van-field 配置 rules 验证规则
    2.当表单的提交事件触发时候，会自动进行表单验证
    当验证通过，会触发表单submit 事件
    当验证失败，不会触发submit事件 -->
    <van-form @submit="onSubmit" ref="loginForm">
      <van-field v-model="user.mobile" name="mobile" placeholder="请输入手机号" :rules="userFormRules.mobile" type="number" maxlength="11">
        <!-- 左侧插入图标 -->
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field v-model="user.code" name="code" placeholder="请输入验证码" :rules="userFormRules.code" type="number" maxlength="6">
        <!-- 输入框插入按钮 -->
        <template #button>
          <!-- 倒计时count-down组件 -->
          <van-count-down :time="1000 * 60" format="ss s" v-if="isCountDownShow" @finish="isCountDownShow = false" />

          <van-button v-else size="small" type="default" round class="send-sms-btn" @click="sendSms" native-type="button">发送验证码</van-button>
        </template>
        <!-- 左侧插入图标 -->
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
      </van-field>
      <!-- 登录按钮 -->
      <div class="login-btn-wrap">
        <van-button class="login-btn" block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>
<script>
import { login, sendSms } from '../../api/user'
export default {
  data() {
    return {
      user: {
        mobile: '',
        code: ''
      },
      userFormRules: {
        mobile: [
          { required: true, message: '手机号不能为空' },
          { pattern: /^1[3|5|7|8]\d{9}$/, message: '手机号格式错误' }
        ],
        code: [
          { required: true, message: '验证码不能为空' },
          { pattern: /^\d{6}$/, message: '验证码格式错误' }
        ]
      },
      isCountDownShow: false
    }
  },
  activated() {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    // 登录
    async onSubmit() {
      const user = this.user
      // 使用Toast轻提示
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true, // 禁用背景点击功能
        durations: 0 // 提示默认持续时间2000，如果为0则为持续展示状态
      })
      // 请求登录
      try {
        const { data } = await login(user)
        this.$store.commit('setUser', data.data)
        this.$toast.success('登录成功') // 新Toast出现会关闭上一次的Toast
        this.$router.back()
        // this.$route.p
      } catch (err) {
        if (err.response.status === 400) {
          console.log('手机或者验证码错误')
          this.$toast.fail('手机或者验证码错误')
        } else {
          console.log('登录失败', err)
          this.$toast.fail('登录失败,请稍后重试')
        }
      }
    },
    // 发送验证码
    async sendSms() {
      // 验证手机号
      try {
        await this.$refs.loginForm.validate('mobile')
        console.log('验证通过')
      } catch (err) {
        return console.log('验证失败', err)
      }
      // 验证通过 显示倒计时
      this.isCountDownShow = true
      // 发送验证码
      try {
        const res = await sendSms(this.user.mobile)
        console.log('发送成功', res)
        this.$toast('发送成功')
      } catch (err) {
        console.log('发送失败', err)
        this.$toast('发送失败')
        // 发送失败关闭倒计时
        this.isCountDownShow = false
      }
    }
  }
}
</script>
<style lang="less" scoped>
.toutiao {
  font-size: 37px;
}
.send-sms-btn {
  font-size: 22px;
  background-color: #ededed;
  width: 156px;
  height: 46px;
  line-height: 46px;
  color: #666;
}
.login-btn-wrap {
  padding: 53px 33px;
  .login-btn {
    background-color: #6db4fb;
    border: none;
    border-radius: 5px;
  }
}
</style>
